package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {
    /*public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    //获取session会话, 是Spring容器提供的
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    //存登录用户
    public static void setCurrentUser(Employee employee){
        getSession().setAttribute(EMPLOYEE_IN_SESSION,employee);
    }

    public static Employee getCurrentUser(){
        return (Employee)getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }

    //存登录用户的权限信息
    public static void setExpression(List<String> permissions){
        getSession().setAttribute(EXPRESSION_IN_SESSION,permissions);
    }

    public static List<String> getExpression(){
        return (List<String>)getSession().getAttribute(EXPRESSION_IN_SESSION);
    }*/

    public static Employee getCurrentUser(){
        return (Employee) SecurityUtils.getSubject().getPrincipal();
    }

}
